﻿using ChartAI.DTO.CzInfo;
using ChartAI.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Furion.LinqBuilder;
using ChartAI.Core.CM;

namespace ChartAI.Application.CzInfo.Services
{
    public class CzInfoServiceDM : ICzInfoServiceDM, ITransient
    {
        ISqlSugarClient c;
        public CzInfoServiceDM(ISqlSugarClient db)
        {
            c = db;
        }
        public ResultPageEntity<CzInfoDTO> GetCzInfoList(Request_CzInfo dto)
        {
            var expr = Expressionable.Create<ChartAI_CzInfo>();


            int count = 0;
            var query = c.Queryable<ChartAI_CzInfo>().Where(expr.ToExpression()).OrderBy(px => px.Money).ToPageList(dto.PageIndex, dto.PageSize, ref count);
            var list = query.Adapt<List<CzInfoDTO>>();
            return new ResultPageEntity<CzInfoDTO>() { Data = list, PageIndex = dto.PageIndex, PageSize = dto.PageSize, Count = count };
        }

        public ResultEntity<bool> AddCzInfo(CzInfoModel model)
        {
            ChartAI_CzInfo tp = c.Queryable<ChartAI_CzInfo>().Where(w => w.Money == model.Money).First();
            if (tp != null)
            {
                return new ResultEntityUtil<bool>().Failure("充值金额已经存在");
            }


            ChartAI_CzInfo data = model.Adapt<ChartAI_CzInfo>();


            data.Id = Guid.NewGuid();


            var result = c.Insertable(data).ExecuteCommand();
            if (result > 0)
            {
                return new ResultEntityUtil<bool>().Success(true);

            }
            else
            {
                return new ResultEntityUtil<bool>().Success(false);
            }
        }

        public ResultEntity<bool> DeCzInfo(Guid id)
        {
            var result = c.Deleteable<ChartAI_CzInfo>().In(id).ExecuteCommand();
            if (result > 0)
            {
                return new ResultEntityUtil<bool>().Success(true);
            }
            else
            {
                return new ResultEntityUtil<bool>().Success(false);
            }
        }

        public ResultEntity<bool> UpCzInfo(CzInfoModel model)
        {
            ChartAI_CzInfo tp = c.Queryable<ChartAI_CzInfo>().Where(w => w.Money == model.Money && w.Id != model.Id).First();
            if (tp != null)
            {
                return new ResultEntityUtil<bool>().Failure("充值金额已经存在");
            }

            var data = c.Queryable<ChartAI_CzInfo>().InSingle(model.Id);
            data.Money = model.Money;
            data.Number = model.Number;

            var result = c.Updateable(data).ExecuteCommand();
            if (result > 0)
            {
                return new ResultEntityUtil<bool>().Success(true);
            }
            else
            {
                return new ResultEntityUtil<bool>().Success(false);
            }
        }
    }
}
